﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Common.Extensions
{
    /// <summary>
    /// 加密解密类
    /// </summary>
    public class EncryptionLib
    {
        /// <summary>
        /// 解密解密8位密钥
        /// </summary>
        protected static string EncryptionKey = "CloudCard";

        /// <summary>
        /// Almighty加密
        /// </summary>
        /// <param name="strTemp"></param>
        /// <returns></returns>
        public static string Encrypt(string strTemp)
        {
            string rs = string.Empty;
            string key = EncryptionKey.Substring(0, 8); //去加密key的前8位
            var provider = new DESCryptoServiceProvider();
            provider.Padding = PaddingMode.Zeros;
            provider.Mode = CipherMode.ECB;
            provider.Key = System.Text.Encoding.ASCII.GetBytes(key);
            provider.IV = System.Text.Encoding.ASCII.GetBytes(key);
            byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strTemp);
            MemoryStream Mstream = new MemoryStream();
            var Cstream = new CryptoStream(Mstream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            Cstream.Write(bytes, 0, bytes.Length);
            Cstream.FlushFinalBlock();
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            foreach (byte num in Mstream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            Mstream.Close();
            rs = builder.ToString();
            return rs;
        }

        /// <summary>
        /// Almighty加密
        /// </summary>
        /// <param name="strTemp"></param>
        /// <returns></returns>
        public static string Encrypt(string strTemp, string key)
        {
            string rs = string.Empty;
            key = key.Substring(0, 8); //去加密key的前8位
            var provider = new DESCryptoServiceProvider();
            provider.Padding = PaddingMode.Zeros;
            provider.Mode = CipherMode.ECB;
            provider.Key = System.Text.Encoding.ASCII.GetBytes(key);
            provider.IV = System.Text.Encoding.ASCII.GetBytes(key);
            byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strTemp);
            MemoryStream Mstream = new MemoryStream();
            var Cstream = new CryptoStream(Mstream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            Cstream.Write(bytes, 0, bytes.Length);
            Cstream.FlushFinalBlock();
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            foreach (byte num in Mstream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            Mstream.Close();
            rs = builder.ToString();
            return rs;
        }

        /// <summary>
        /// Almighty解密
        /// </summary>
        /// <param name="strTemp"></param>
        /// <returns></returns>
        public static string Decrypt(string strTemp)
        {
            string rs = string.Empty;
            string key = EncryptionKey.Substring(0, 8);
            try
            {
                var provider = new DESCryptoServiceProvider();
                provider.Padding = PaddingMode.Zeros;
                provider.Mode = CipherMode.ECB;
                provider.Key = System.Text.Encoding.ASCII.GetBytes(key);
                provider.IV = System.Text.Encoding.ASCII.GetBytes(key);
                byte[] buffer = new byte[strTemp.Length / 2];
                for (int i = 0; i < (strTemp.Length / 2); i++)
                {
                    int num = Convert.ToInt32(strTemp.Substring(i * 2, 2), 0x10);
                    buffer[i] = (byte)num;
                }
                MemoryStream Mstream = new MemoryStream();
                var Cstream = new CryptoStream(Mstream, provider.CreateDecryptor(), CryptoStreamMode.Write);
                Cstream.Write(buffer, 0, buffer.Length);
                Cstream.FlushFinalBlock();
                Mstream.Close();
                rs = System.Text.Encoding.GetEncoding("GB2312").GetString(Mstream.ToArray()).TrimEnd('\0');
            }
            catch
            {
                rs = "-1";
            }
            return rs;
        }

        /// <summary>
        /// Almighty解密
        /// </summary>
        /// <param name="strTemp"></param>
        /// <returns></returns>
        public static string Decrypt(string strTemp, string key)
        {
            string rs = string.Empty;
            key = key.Substring(0, 8);
            try
            {
                var provider = new DESCryptoServiceProvider();
                provider.Padding = PaddingMode.Zeros;
                provider.Mode = CipherMode.ECB;
                provider.Key = System.Text.Encoding.ASCII.GetBytes(key);
                provider.IV = System.Text.Encoding.ASCII.GetBytes(key);
                byte[] buffer = new byte[strTemp.Length / 2];
                for (int i = 0; i < (strTemp.Length / 2); i++)
                {
                    int num = Convert.ToInt32(strTemp.Substring(i * 2, 2), 0x10);
                    buffer[i] = (byte)num;
                }
                MemoryStream Mstream = new MemoryStream();
                var Cstream = new CryptoStream(Mstream, provider.CreateDecryptor(), CryptoStreamMode.Write);
                Cstream.Write(buffer, 0, buffer.Length);
                Cstream.FlushFinalBlock();
                Mstream.Close();
                rs = System.Text.Encoding.GetEncoding("GB2312").GetString(Mstream.ToArray()).TrimEnd('\0');
            }
            catch
            {
                rs = "-1";
            }
            return rs;
        }
    }
}
